class tree.io.rpc.JSON_Proxy extends tree.io.rpc.Proxy
	###
	Proxy for xmlrpc server methods.
	For the standard look here: http://www.xmlrpc.com/spec.
	
	It also needs theese extensions:
	
	http://xmlrpc-c.sourceforge.net/introspection.html
	http://ontosys.com/xml-rpc/extensions.php
	
	events: ['error-occured'],
	###
	
	constructor: (url, version) ->
	    super url
	    
	    if(!version)
	        version = '1.0'
	        
	    @version = version
	    @content_type = tree.io.ajax.DATA_FORMAT.json
    
    parse_response: (data) ->
        response = tree.io.formats.json.load data
        ###        
        example: { "result": "Hello JSON-RPC", "error": null, "id": 1}
        ###
        if @version == '1.0'
            if response.error
                @emit 'error-occured', {error: response.error}
                return

            return response.result
        
            ###
            { 
                "version": "1.1", 
                "result": "done", 
                "error": null, 
                "id": "194521489"
            }
            ###
        else if @version == '1.1'
            if response.version != '1.1'
                throw new "invalid version in response: #{response.version}"
                
            if response.error
                @emit 'error-occured', {error: response.error}
                return
                
            return response.result
        
        else if @version == '2.0'
            
        else
            throw "invalid json-rpc version: #{@version}"
    
    create_request: (name, params) ->
        request = {}
        ###
        example: { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
        ###
        if @version == '1.0'
            request.id = 1
            request.method = name
            request.params = params
        
            ###
            { 
                "version": "1.1", 
                "method": "confirmFruitPurchase", 
                "id": "194521489", 
                "params": 
                [ 
                    [ "apple", "orange", "pear" ], 
                    1.123 
                ]
            }
            ###
        else if @version == '1.1'
            request.id = 1
            request.version = '1.1'
            request.method = name
            request.params = params
        
        else if @version == '2.0'
            
        
        else
            throw "invalid json-rpc version: #{@version}"
        
        return tree.io.formats.json.dump request
